// pages/events/zicha/add/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js")
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    submitState:false,
    content:'',
    fileList1: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  bindInput(e){
    console.log('bindInput',e)
	  this.data.content = e.detail.value;
  },
  goAdd(){
    var estate_id = app.getStorageSync("estate_id");
    if(estate_id == 0){
      this.handleEstate()
    }else{
      this.submitOp();
    }
  },
  handleEstate(){
    var that = this;
    var message = "请绑定小区后再进行购买"
    Dialog.confirm({
      title: '绑定小区',
      message
    }).then(() => {
      wx.navigateTo({
        url: '/pages/account/index?from_type=chat',
      })
    })
    .catch(() => {
      Dialog.close();
    });
  },
  submitOp:function(){
    this.handleEstate();
    var that = this;
    if (this.data.content.length <= 0){
      Toast.fail('请输入存在问题');
      return;
    }
    if(that.data.submitState){
      Toast.fail('正在提交...');
      return;
    }
    that.data.submitState = true;
    that.setData({submitState : true })

    fetchApi.httpPost('/estate/chat/add', {
      content: that.data.content,
    }, function (res) {
      console.log("物业接待上报返回数据", res)
      if (res.data.code == 0) {
        var chat_id = res.data.data;
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.success(res.data.msg);
        if (chat_id > 0 && that.data.fileList1.length > 0) {
          for (var index in that.data.fileList1) {
            wx.uploadFile({
            url: fetchApi.getPlatformUrl() + '/estate/chat/uploadImg',
            filePath: that.data.fileList1[index].url,
            name: 'file',
            formData: {
              chat_id: chat_id,
              sort:index,
              img_type:0,
            },
            success: function (res) {
              console.log('uploadFile success res', res)
            }, fail: function (res) {
              console.log('uploadFile fail res', res)
            }
            })
          }
        }
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 1000)
      }else{
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
  deleteImg:function(res){
    console.log('删除图片', res)
    var index = res.currentTarget.dataset.index;
    this.data.fileList1 = [];
    this.setData({
      fileList1: this.data.fileList1
    });
  },
  afterRead(event) {
    console.log('afterRead',event)
    const { file, name } = event.detail;
    console.log('afterRead name',name)
    console.log('afterRead file',file)
    const fileList = this.data[`fileList${name}`];
    
    this.setData({ [`fileList${name}`]: fileList.concat(file) });
    
    console.log('afterRead fileList',fileList)
    this.data.fileList1.concat(file)
    console.log('fileList',this.data.fileList1)
  },
  oversize() {
	  wx.showToast({ title: '文件超出大小限制', icon: 'none' });
  },
})